package com.wsc.wsckeeper.adapter;

import java.io.File;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wsc.wsckeeper.R;

public class BackupAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private List<File> files;

	public BackupAdapter(Context context, List<File> files) {
		this.files = files;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;

		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.adapter_backup, null);
			holder.time = (TextView) convertView.findViewById(R.id.item_time);
			holder.date = (TextView) convertView.findViewById(R.id.item_date);
			holder.name = (TextView) convertView.findViewById(R.id.item_name);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		File item = files.get(position);
		if (item != null) {
			String name = item.getName();
			int index = name.indexOf("_");
			holder.name.setText(name.substring(0, index));

			String date = name.substring(index + 1, index + 11).replace("_", "/");
			holder.date.setText(date);

			String time = name.substring(index + 12, index + 20).replace("_", ":");
			holder.time.setText(time);
		}

		// Animation anim = AnimationUtils.loadAnimation(context,
		// R.anim.anim_list_view);
		// convertView.startAnimation(anim);

		return convertView;
	}

	class ViewHolder {
		TextView time;
		TextView date;
		TextView name;
	}

	@Override
	public int getCount() {
		return files.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
}